package eventdisplay;

import eventdisplay.EventDisplayApplet;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.*;
import javax.swing.plaf.SplitPaneUI;
import javax.swing.plaf.basic.BasicBorders.SplitPaneBorder;
import javax.swing.*;

import remixlab.proscene.Camera;

import java.awt.Dimension;
import java.util.Timer;
import java.util.TimerTask;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class DispController extends javax.swing.JPanel {

    private JCheckBox mod1CheckBox = null;
    private JCheckBox mod2CheckBox = null;
    private JCheckBox holeCheckBox = null;
    private JCheckBox veto1CheckBox = null;
    private JCheckBox veto2CheckBox = null;
    private JCheckBox veto3CheckBox = null;
    private JCheckBox trackCheckBox = null;
    private JCheckBox infoCheckBox = null;
    private JCheckBox reloadCheckBox = null;
    private JButton rewButton = null;
    private JButton playButton = null;
    private JButton ffButton = null;
    private JButton reloadButton = null;
    private JButton aboutButton = null;
    public static String versionString = "Ver. 2.120511";
    public ImageIcon playIcon = new ImageIcon(getClass().getResource("/eventdisplay/icons/play.png"));  //  @jve:decl-index=0:
    public ImageIcon pauseIcon = new ImageIcon(getClass().getResource("/eventdisplay/icons/pause.png"));
    public ImageIcon ffIcon = new ImageIcon(getClass().getResource("/eventdisplay/icons/ff.png"));
    public ImageIcon rewIcon = new ImageIcon(getClass().getResource("/eventdisplay/icons/rew.png"));
    public ImageIcon refreshIcon = new ImageIcon(getClass().getResource("/eventdisplay/icons/refresh.png"));
    private JRadioButton perspRadioButton = null;
    private JRadioButton orthoRadioButton = null;
    private JSlider rateSlider = null;
    private JComboBox eventsListComboBox = null;
    private JTextField periodTextField = null;
    public String[] names;
    boolean autoReload;
    int nSec;
    int period = 10;
    private int prefW = 1024, prefH = 576;
    private int contW = 170, contH = prefH;
    private static int w = 830;
    private static int h = 535;
    private EventDisplayApplet eventDisplayApplet;
    static JPanel appletPanel;
    Timer timer = new Timer();
    
//    public ImageIcon splashIcon = new ImageIcon(getClass().getResource("/eventdisplay/splash.png"));
//    public Image splashImage = splashIcon.getImage();


    
    
    static SplashScreen mySplash;                   // instantiated by JVM we use it to get graphics
    static Graphics2D splashGraphics;               // graphics context for overlay of the splash image
    static Rectangle2D.Double splashTextArea;       // area where we draw the text
    static Rectangle2D.Double splashProgressArea;   // area where we draw the progress bar
    static Font font;                               // used to draw our text
    
    final TimerTask myTask = new TimerTask() {

        public void run() {

            if (autoReload) {
                nSec++;
                //System.out.println(nSec);
                if (nSec % period == 0) {
                    Reload();
                    nSec = 0;
                }
            }
        }
    };
    

    private void setInfo(TrackData track) {
        runLabel.setText(" " + track.run);
        snarlLabel.setText(" " + track.snarl);
        dateLabel.setText(" " + track.date);
        timeLabel.setText(" " + track.time);
    }

    private void Reload() {
        eventDisplayApplet.len = 0;
        eventDisplayApplet.track = null;
        eventDisplayApplet.track = new TrackData(eventDisplayApplet, eventDisplayApplet.filename + "?uniq=" + Math.round(Math.random() * 10000));
        eventDisplayApplet.tracelength = eventDisplayApplet.track.particleTrace.length;
        setInfo(eventDisplayApplet.track);
    }

    public DispController() {
        

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

        // ============================================================

        eventDisplayApplet = new EventDisplayApplet(w, h);
        eventDisplayApplet.init();
        eventDisplayApplet.filename = eventDisplayApplet.tracklistobj.listOfUrls[0];

        JPanel appletPanel = new JPanel(new GridLayout(0, 1));
        appletPanel.setBorder(BorderFactory.createTitledBorder(""));
        appletPanel.add(eventDisplayApplet);
        // -------------------------------------------------------------

        JPanel reloadPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c1 = new GridBagConstraints();
        reloadPanel.add(getEventsListComboBox(), c1);
        reloadPanel.add(getReloadButton(), c1);

        JPanel autoPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c2 = new GridBagConstraints();
        autoPanel.add(getReloadCheckBox(), c2);
        autoPanel.add(getPeriodTextField(), c2);
        autoPanel.add(sUnitLabel, c2);

        JPanel eventPanel = new JPanel(new GridLayout(0, 1));
        eventPanel.setBorder(BorderFactory.createTitledBorder("Event"));
        Dimension eventPanelSize = new Dimension(165, 180);
        eventPanel.setPreferredSize(eventPanelSize);
        eventPanel.setMinimumSize(eventPanelSize);
        eventPanel.setMaximumSize(eventPanelSize);
        eventPanel.add(freezeLabel);
        eventPanel.add(dateLabel);
        eventPanel.add(reloadPanel);
        eventPanel.add(runLabel);
        eventPanel.add(snarlLabel);
        eventPanel.add(dateLabel);
        eventPanel.add(timeLabel);
        eventPanel.add(autoPanel);

        Reload();

        // -------------------------------------------------------------

        JPanel ratePanel = new JPanel(new GridBagLayout());
        GridBagConstraints c3 = new GridBagConstraints();
        ratePanel.add(rateLabel, c3);
        ratePanel.add(getRateSlider(), c3);

        JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
        buttonPanel.add(getRewButton());
        buttonPanel.add(getPlayButton());
        buttonPanel.add(getFfButton());

        JPanel viewPanel = new JPanel(new GridLayout(1, 0));
        viewPanel.add(getPerspRadioButton());
        viewPanel.add(getOrthoRadioButton());
        ButtonGroup group = new ButtonGroup();
        group.add(getPerspRadioButton());
        group.add(getOrthoRadioButton());

        JPanel displayPanel = new JPanel(new GridLayout(0, 1));
        displayPanel.setBorder(BorderFactory.createTitledBorder("Draw options"));
        Dimension displayPanelSize = new Dimension(165, 120);
        displayPanel.setPreferredSize(displayPanelSize);
        displayPanel.setMinimumSize(displayPanelSize);
        displayPanel.setMaximumSize(displayPanelSize);
        displayPanel.add(ratePanel);
        displayPanel.add(buttonPanel);
        displayPanel.add(viewPanel);
        

        // -------------------------------------------------------------		

        JPanel filterPanel = new JPanel(new GridLayout(0, 1));
        filterPanel.setBorder(BorderFactory.createTitledBorder("Filter"));
        Dimension filterPanellSize = new Dimension(165, 210);
        filterPanel.setPreferredSize(filterPanellSize);
        filterPanel.setMinimumSize(filterPanellSize);
        filterPanel.setMaximumSize(filterPanellSize);
        filterPanel.add(getMod1CheckBox());
        filterPanel.add(getMod2CheckBox());
        filterPanel.add(getHoleCheckBox());
        filterPanel.add(getVeto1CheckBox());
        filterPanel.add(getVeto2CheckBox());
        filterPanel.add(getVeto3CheckBox());
        filterPanel.add(getTrackCheckBox());
        filterPanel.add(getInfoCheckBox());

        // =======================================================

        JPanel toolsPanel = new JPanel();
        toolsPanel.setLayout(new BoxLayout(toolsPanel, BoxLayout.PAGE_AXIS));
        // assemble tools pane
        toolsPanel.add(eventPanel);
        toolsPanel.add(displayPanel);
        toolsPanel.add(filterPanel);
        toolsPanel.add(getAboutButton());
        
        //Create a split pane with the two scroll panes in it.
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, toolsPanel, appletPanel);
        splitPane.setDividerLocation(170);
        splitPane.setDividerSize(5);
        splitPane.setOneTouchExpandable(false);

        //Provide minimum sizes for the two components in the split pane
        //Dimension toolSize = new Dimension(165, prefH);
        //toolsPanel.setPreferredSize(toolSize);
        //toolsPanel.setMinimumSize(new Dimension(0, prefH));
        //toolsPanel.setMaximumSize(toolSize);

        Dimension appletMinSize = new Dimension(5000, h);
        appletPanel.setMinimumSize(appletMinSize);

        add(splitPane);

        timer.scheduleAtFixedRate(myTask, 0, 1000);

    }

    private JCheckBox getMod1CheckBox() {
        if (mod1CheckBox == null) {
            mod1CheckBox = new JCheckBox();
            mod1CheckBox.setText("Supermodule 1");
            mod1CheckBox.setSelected(true);
            mod1CheckBox.addItemListener(new java.awt.event.ItemListener() {

                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    // System.out.println("itemStateChanged()"); // TODO
                    // Auto-generated Event stub itemStateChanged()
                    eventDisplayApplet.isModule1Drawn = mod1CheckBox.isSelected();
                }
            });
        }
        return mod1CheckBox;
    }

    private JCheckBox getMod2CheckBox() {
        if (mod2CheckBox == null) {
            mod2CheckBox = new JCheckBox();
            mod2CheckBox.setText("Supermodule 2");
            mod2CheckBox.setSelected(true);
            mod2CheckBox.addItemListener(new java.awt.event.ItemListener() {

                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    // System.out.println("itemStateChanged()"); // TODO
                    // Auto-generated Event stub itemStateChanged()
                    eventDisplayApplet.isModule2Drawn = mod2CheckBox.isSelected();
                }
            });
        }
        return mod2CheckBox;
    }

    private JCheckBox getHoleCheckBox() {
        if (holeCheckBox == null) {
            holeCheckBox = new JCheckBox();
            holeCheckBox.setText("Holes");
            holeCheckBox.setSelected(true);
            holeCheckBox.addItemListener(new java.awt.event.ItemListener() {

                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    // System.out.println("itemStateChanged()"); // TODO
                    // Auto-generated Event stub itemStateChanged()
                    eventDisplayApplet.isHoleDrawn = holeCheckBox.isSelected();
                }
            });
        }
        return holeCheckBox;
    }

    private JCheckBox getVeto1CheckBox() {
        if (veto1CheckBox == null) {
            veto1CheckBox = new JCheckBox();
            veto1CheckBox.setText("VetoShield 1");
            veto1CheckBox.setSelected(true);
            veto1CheckBox.addItemListener(new java.awt.event.ItemListener() {

                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    // System.out.println("itemStateChanged()"); // TODO
                    // Auto-generated Event stub itemStateChanged()
                    eventDisplayApplet.isvShld1Drawn = veto1CheckBox.isSelected();
                }
            });
        }
        return veto1CheckBox;
    }

    private JCheckBox getVeto2CheckBox() {
        if (veto2CheckBox == null) {
            veto2CheckBox = new JCheckBox();
            veto2CheckBox.setText("VetoShield 2");
            veto2CheckBox.setSelected(true);
            veto2CheckBox.addItemListener(new java.awt.event.ItemListener() {

                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    // System.out.println("itemStateChanged()"); // TODO
                    // Auto-generated Event stub itemStateChanged()
                    eventDisplayApplet.isvShld2Drawn = veto2CheckBox.isSelected();
                }
            });
        }
        return veto2CheckBox;
    }

    private JCheckBox getVeto3CheckBox() {
        if (veto3CheckBox == null) {
            veto3CheckBox = new JCheckBox();
            veto3CheckBox.setText("VetoShield 3");
            veto3CheckBox.setSelected(true);
            veto3CheckBox.addItemListener(new java.awt.event.ItemListener() {

                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    // System.out.println("itemStateChanged()"); // TODO
                    // Auto-generated Event stub itemStateChanged()
                    eventDisplayApplet.isvShld3Drawn = veto3CheckBox.isSelected();
                }
            });
        }
        return veto3CheckBox;
    }

    private JCheckBox getTrackCheckBox() {
        if (trackCheckBox == null) {
            trackCheckBox = new JCheckBox();
            trackCheckBox.setText("Tracks");
            trackCheckBox.setSelected(true);
            trackCheckBox.addItemListener(new java.awt.event.ItemListener() {

                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    // System.out.println("itemStateChanged()"); // TODO
                    // Auto-generated Event stub itemStateChanged()
                    eventDisplayApplet.isTrackDrawn = trackCheckBox.isSelected();
                }
            });
        }
        return trackCheckBox;
    }

    private JCheckBox getInfoCheckBox() {
        if (infoCheckBox == null) {
            infoCheckBox = new JCheckBox();
            infoCheckBox.setSelected(false);
            infoCheckBox.setText("Info");
            infoCheckBox.addItemListener(new java.awt.event.ItemListener() {

                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    //System.out.println("itemStateChanged()"); // TODO Auto-generated Event stub itemStateChanged()
                    eventDisplayApplet.isInfoDrawn = infoCheckBox.isSelected();
                    eventDisplayApplet.scene.setAxisIsDrawn(infoCheckBox.isSelected());
                    eventDisplayApplet.scene.setGridIsDrawn(infoCheckBox.isSelected());
                }
            });
        }
        return infoCheckBox;
    }

    private JButton getRewButton() {
        if (rewButton == null) {
            rewButton = new JButton();
            rewButton.setDoubleBuffered(true);
            rewButton.setIcon(rewIcon);
            rewButton.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    eventDisplayApplet.len = 0;
                }
            });
        }
        return rewButton;
    }

    private JButton getPlayButton() {
        if (playButton == null) {
            playButton = new JButton();
            playButton.setIcon(pauseIcon);
            playButton.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    // System.out.println("actionPerformed()"); // TODO
                    // Auto-generated Event stub actionPerformed()
                    eventDisplayApplet.inc = (int) ((eventDisplayApplet.inc != 1) ? 1 : 0);
                    playButton.setIcon(((eventDisplayApplet.inc != 1) ? playIcon : pauseIcon));
                }
            });
        }
        return playButton;
    }

    private JButton getFfButton() {
        if (ffButton == null) {
            ffButton = new JButton();
            ffButton.setIcon(ffIcon);
            ffButton.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    // System.out.println("actionPerformed()"); // TODO
                    // Auto-generated Event stub actionPerformed()
                    eventDisplayApplet.len = eventDisplayApplet.tracelength;
                }
            });
        }
        return ffButton;
    }

    private JRadioButton getPerspRadioButton() {
        if (perspRadioButton == null) {
            perspRadioButton = new JRadioButton();
            perspRadioButton.setText("Persp");
            perspRadioButton.setSelected(false);
            perspRadioButton.addItemListener(new java.awt.event.ItemListener() {

                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    //System.out.println("itemStateChanged()"); // TODO Auto-generated Event stub itemStateChanged()
                    eventDisplayApplet.scene.camera().setType(Camera.Type.PERSPECTIVE);
                }
            });
        }
        return perspRadioButton;
    }

    private JRadioButton getOrthoRadioButton() {
        if (orthoRadioButton == null) {
            orthoRadioButton = new JRadioButton();
            orthoRadioButton.setEnabled(true);
            orthoRadioButton.setSelected(true);
            orthoRadioButton.setText("Ortho");
            orthoRadioButton.addItemListener(new java.awt.event.ItemListener() {

                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    //System.out.println("itemStateChanged()"); // TODO Auto-generated Event stub itemStateChanged()
                    eventDisplayApplet.scene.camera().setType(Camera.Type.ORTHOGRAPHIC);
                }
            });
        }
        return orthoRadioButton;
    }
    private JLabel rateLabel = new JLabel("Rate");

    private JSlider getRateSlider() {
        if (rateSlider == null) {
            rateSlider = new JSlider();
            rateSlider.setOrientation(JSlider.HORIZONTAL);
            //rateSlider.setBorder(BorderFactory.createTitledBorder("Draw rate"));
            rateSlider.setValue(5);
            rateSlider.setMinorTickSpacing(1);
            rateSlider.setMajorTickSpacing(1);
            rateSlider.setPaintTicks(true);
            rateSlider.setPaintLabels(false);
            rateSlider.setSnapToTicks(true);
            rateSlider.setMinimum(1);
            rateSlider.setMaximum(5);
            rateSlider.setPreferredSize(new Dimension(100, 50));
            rateSlider.setMinimumSize(new Dimension(115, 50));
            rateSlider.addChangeListener(new javax.swing.event.ChangeListener() {

                public void stateChanged(javax.swing.event.ChangeEvent e) {
                    // System.out.println("stateChanged()" +
                    // jSlider.getValue()+1); // TODO Auto-generated Event stub
                    // stateChanged()
                    // jSlider.getValue()
                    eventDisplayApplet.Fs = 6 - rateSlider.getValue();
                    //String[] names = eventDisplayApplet.listOfFiles;
                }
            });
        }
        return rateSlider;
    }
    private JLabel freezeLabel = new JLabel(versionString);

    private JComboBox getEventsListComboBox() {
        if (eventsListComboBox == null) {
            eventsListComboBox = new JComboBox();
            eventsListComboBox.setEditable(true);
            names = eventDisplayApplet.tracklistobj.listOfFiles;
            for (int i = 0; i < names.length; i++) {
                eventsListComboBox.insertItemAt(names[i], i);
            }
            //eventsListComboBox.setPreferredSize(new Dimension(50,20));
            eventsListComboBox.setSelectedIndex(0);
            //eventsListComboBox.setMaximumSize(new Dimension(150,20));
            eventsListComboBox.setMinimumSize(new Dimension(120, 20));

            eventsListComboBox.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    //eventDisplayApplet.filename = (String) eventsListComboBox.getSelectedItem();
                    eventDisplayApplet.filename = eventDisplayApplet.tracklistobj.listOfUrls[eventsListComboBox.getSelectedIndex()];
                    //System.out.println(eventDisplayApplet.filename);
                    eventDisplayApplet.len = 0;
                    //eventDisplayApplet.Fn = 0;
                    eventDisplayApplet.track = new TrackData(eventDisplayApplet, eventDisplayApplet.filename + "?uniq=" + Math.round(Math.random() * 10000));
                    eventDisplayApplet.tracelength = eventDisplayApplet.track.particleTrace.length;
                    //eventDisplayApplet.tracelength = 0;
                    setInfo(eventDisplayApplet.track);
                }
            });
        }
        return eventsListComboBox;
    }

    private JButton getReloadButton() {
        if (reloadButton == null) {
            reloadButton = new JButton();
            reloadButton.setIcon(refreshIcon);
            reloadButton.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    //System.out.println(!(eventDisplayApplet.len < eventDisplayApplet.tracelength));
                    Reload();
                }
            });
        }
        return reloadButton;
    }
    private JLabel runLabel = new JLabel(" Run");
    private JLabel snarlLabel = new JLabel(" Snarl");
    private JLabel dateLabel = new JLabel(" Date");
    private JLabel timeLabel = new JLabel(" Time");

    private JCheckBox getReloadCheckBox() {
        if (reloadCheckBox == null) {
            reloadCheckBox = new JCheckBox();
            reloadCheckBox.setText("Update every");
            reloadCheckBox.setActionCommand("Auto update");
            reloadCheckBox.setEnabled(true);
            reloadCheckBox.setName("reloadCheckBox");
            reloadCheckBox.setSelected(false);
            reloadCheckBox.addItemListener(new java.awt.event.ItemListener() {

                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    autoReload = reloadCheckBox.isSelected();
                }
            });
        }
        return reloadCheckBox;
    }

    private JTextField getPeriodTextField() {
        if (periodTextField == null) {
            periodTextField = new JTextField();
            periodTextField.setText("10");
            periodTextField.setPreferredSize(new Dimension(30, 20));
            periodTextField.setMinimumSize(new Dimension(30, 20));
            periodTextField.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    //System.out.println(jTextField.getText()); // TODO Auto-generated Event stub actionPerformed()
                    period = Integer.parseInt(periodTextField.getText());
                }
            });
        }
        return periodTextField;
    }
    private JLabel sUnitLabel = new JLabel("s");
    
    private aboutJWindow abtframe = new aboutJWindow(versionString);
    
    private JButton getAboutButton() {
        if (aboutButton == null) {
            aboutButton = new JButton("About");

            aboutButton.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    javax.swing.SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                                abtframe.openWindow();

                        }
                    });

                }
            });
        }
        return aboutButton;
    }

    public static void createGUI() {
        // create new JFrame
        final JFrame jf = new JFrame("Live Event Display MINOS");
        
        // this allows program to exit
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jf.getContentPane().add(new DispController());

        jf.setSize(1024, 576);

        jf.setVisible(true);
        
    }

    private static void appInit() {
        for (int i = 1; i <= 100; i++) {   // pretend we have 10 things to do
            int pctDone = i * 1;       // this is about the only time I could calculate rather than guess progress
            //splashText("Doing task #" + i);     // tell the user what initialization task is being done
            splashProgress(pctDone);            // give them an idea how much we have completed
            try {
                Thread.sleep(10);             // wait a second
            } catch (InterruptedException ex) {
                break;
            }
        }
    }

//Prepare the global variables for the other splash functions

    private static void splashInit() {
        // the splash screen object is created by the JVM, if it is displaying a splash image

        mySplash = SplashScreen.getSplashScreen();
        // if there are any problems displaying the splash image
        // the call to getSplashScreen will returned null

        if (mySplash != null) {
            // get the size of the image now being displayed
            Dimension ssDim = mySplash.getSize();
            int height = ssDim.height;
            int width = ssDim.width;

            // stake out some area for our status information
            splashTextArea = new Rectangle2D.Double(width * 0.755, height*0.8, width * .45, 32.);
            splashProgressArea = new Rectangle2D.Double(width * 0.1, height * .86, width * .8, 4);

            // create the Graphics environment for drawing status info
            splashGraphics = mySplash.createGraphics();
            font = new Font("Dialog", Font.PLAIN, 11);
            splashGraphics.setFont(font);

            // initialize the status info
            splashText(versionString);
            splashProgress(0);
        }
    }

//Display text in status area of Splash.  Note: no validation it will fit. @param str - text to be displayed
    public static void splashText(String str) {
        if (mySplash != null && mySplash.isVisible()) {   // important to check here so no other methods need to know if there
            // really is a Splash being displayed

            // erase the last status text
            //splashGraphics.setPaint(Color.LIGHT_GRAY);
            //splashGraphics.fill(splashTextArea);

            // draw the text
            splashGraphics.setPaint(new Color(255, 215, 0, 250));
            splashGraphics.drawString(str, (int) (splashTextArea.getX() + 10), (int) (splashTextArea.getY() + 15));

            // make sure it's displayed
            mySplash.update();
        }
    }

//Display a (very) basic progress bar @param pct how much of the progress bar to display 0-100

    public static void splashProgress(int pct) {
        if (mySplash != null && mySplash.isVisible()) {

            // Note: 3 colors are used here to demonstrate steps
            // erase the old one
            //splashGraphics.setPaint(Color.LIGHT_GRAY);
            //splashGraphics.fill(splashProgressArea);

            // draw an outline
            //splashGraphics.setPaint(Color.BLUE);
            splashGraphics.setPaint(new Color(255, 215, 0, 50));
            splashGraphics.draw(splashProgressArea);

            // Calculate the width corresponding to the correct percentage
            int x = (int) splashProgressArea.getMinX();
            int y = (int) splashProgressArea.getMinY();
            int wid = (int) splashProgressArea.getWidth();
            int hgt = (int) splashProgressArea.getHeight();

            int doneWidth = Math.round(pct * wid / 100.f);
            doneWidth = Math.max(0, Math.min(doneWidth, wid - 1));  // limit 0-width

            // fill the done part one pixel smaller than the outline
            splashGraphics.setPaint(new Color(255, 215, 0, 255));
            splashGraphics.fillRect(x + 2, y + 3, doneWidth - 2, hgt - 3);

            // make sure it's displayed
            mySplash.update();
        }
    }

    public static void main(String[] args) {
        // threadsafe way to create a Swing GUI
        splashInit();           // initialize splash overlay drawing parameters
        appInit();              // simulate what an application would do before starting
        if (mySplash != null) // check if we really had a spash screen
        {
            mySplash.close();   // we're done with it
        }
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createGUI();
            }
        });
    }
}




